package com.yanqu.road.web.mgr;

import com.yanqu.road.logic.bussiness.log.LogBussiness;
import com.yanqu.road.entity.log.LogUserInfo;
import com.yanqu.road.server.TempMgr;
import org.apache.commons.collections.CollectionUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.List;

public class LogMgr extends TempMgr {

	private static Logger log = LogManager.getLogger(LogMgr.class.getName());

	private static List<LogUserInfo> logUserInfoList;

	public static void addUserInfo(long serverId, long userId, String openId, String nickName, long registerTime,
								   String mac, String ip, int phonePlatform, long originalServerId) {
		LogUserInfo log = new LogUserInfo(serverId, userId, openId, nickName, registerTime, mac, ip, phonePlatform, originalServerId);
		logUserInfoList.add(log);
	}

	//TODO server
	private static void saveLogUserInfoList() {
		if (CollectionUtils.isEmpty(logUserInfoList)) {
			return;
		}
		List<LogUserInfo> list;
		synchronized (logUserInfoList) {
			list = new ArrayList<>(logUserInfoList);
			logUserInfoList.clear();
		}
		LogBussiness.addLogUserInfoList(list);
	}

	/**
	 * 定时保存
	 */
	public boolean save() {
		saveLogUserInfoList();
		return true;
	}

	public boolean stop() {
		return true;
	}

	@Override
	public boolean reloadData() {
		logUserInfoList = new ArrayList<>();
		return true;
	}

	@Override
	public boolean init() throws Exception {
		return reload();
	}

	@Override
	public boolean reloadConfig() throws Exception {
		return true;
	}
}
